

var objectPrototype = Object.prototype;
var toString = objectPrototype.toString;
var enumerables = true;
var enumerablesTest = { toString: 1 };
    
    
var callOverrideParent = function () {
    var method = callOverrideParent.caller.caller; 
    return method.$owner.prototype[method.$name].apply(this, arguments);
}


for (var i in enumerablesTest) {
    enumerables = null;
}

if (enumerables) {
    enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable',
                   'toLocaleString', 'toString', 'constructor'];
}

/**
 * 判断变量是否为对象
 */
export function isObject(value) {
  return value !== null && typeof value === 'object';
};

/**
 * 判断变量是否为数组
 */
export function isArray(value) {
  return Array.isArray(value);
};

/**
 * 给对象的属性赋值
 */
export function apply(object, config, defaults) {
    if (defaults) {
        apply(object, defaults);
    }

    if (object && config && typeof config === 'object') {
        var i, j, k;

        for (i in config) {
            object[i] = config[i];
        }

        if (enumerables) {
            for (j = enumerables.length; j--;) {
                k = enumerables[j];
                if (config.hasOwnProperty(k)) {
                    object[k] = config[k];
                }
            }
        }
    }

    return object;
}

/**
 * 给对象的属性赋值，当对象的属性无值的时候
 */
export function applyIf(object, config) {
    var property;

    if (object) {
        for (property in config) {
            if (object[property] === undefined) {
                object[property] = config[property];
            }
        }
    }
    return object;
}


/*
 * 对象原型继承
 */
export function extend(obj){
    if(typeof Object.create === 'function'){
        return Object.create(obj || {});
    }
    var F = function(){}
    F.prototype = obj;
    return new F();
}


var originJSON = JSON || {
    stringify: function () {
    },
    parse: function () {
    }
};
var stringifyFun = originJSON.stringify;
var parseFun = originJSON.parse;

//获取对象的属性列表
var getObjectPropertyNames = function(obj){
    
    if(!obj || (obj.hasOwnProperty('constructor') && obj.constructor == Object)){
        return;
    }
    var names = Object.getOwnPropertyNames(obj) || new Array();

    var pNames = getObjectPropertyNames(obj.prototype || obj.__proto__);

    if(pNames && pNames.length > 0){
        for(var n=0;n<pNames.length;n++){
            names.push(pNames[n]);
        }
    }
    return names;
}

//对象深 copy
var stringifyImpl = function(obj){
    if(!obj || typeof obj != 'object'){
        return obj;
    }
    if(obj.constructor == Array){
        var newArr = new Array();
        for(var n=0;n<obj.length;n++){
            newArr.push(stringifyImpl(obj[n]));
        }
        return newArr;
    }
    var names = getObjectPropertyNames(obj) || new Array();
    var newObj = {};

    for(var n=0;n<names.length;n++){
        newObj[names[n]] = stringifyImpl(obj[names[n]]);
    }
    return newObj;
}

/**
 *  重写 JavaScript 自带 JSON，解决 Json 序列化时“原型链上的属性丢失”问题
 *  
 */
export var JSON = {
    stringify: function(obj,filter,indent){
        return stringifyFun(stringifyImpl(obj),filter,indent);
    },
    parse: function(s){
        return parseFun.call(originJSON,s);
    }
}

/**
 * 判断变量是否为函数
 */
export function isFunction(funcName) {
    try {
        if (typeof(eval(funcName)) == "function") {
            return true;
        }
    } catch(e) {}
    return false;
}

/**
 * 判断变量是否存在
 */
export function isExits(variableName) {
    try {
        if (typeof(variableName) == "undefined") {
            return false;
        } else {
            return true;
        }
    } catch(e) {}
    return false;
}


/**
 * 利用异步函数，达到程序睡眠的效果，实际上并没有发生真正的线程睡眠
 * @param ms 发毫秒为单位，默认为 0 毫秒
 */
export function sleep(ms = 0) {
  return new Promise((resolve, reject) => setTimeout(resolve, ms));
}
